package com.sgcc.ln.pmos.e_ln_personalcenter.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.IntentUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.sgcc.ln.pmos.arouter.personalcenter.PersonalCenterArouterPath;
import com.sgcc.ln.pmos.common.base.BasePresenter;
import com.sgcc.ln.pmos.common.base.MvpBaseActivity;
import com.sgcc.ln.pmos.common.bean.NoDataResponse;
import com.sgcc.ln.pmos.common.http.HttpInterConst;
import com.sgcc.ln.pmos.common.http.HttpRequestUtils;
import com.sgcc.ln.pmos.common.http.MvcNetCallback;
import com.sgcc.ln.pmos.common.utils.DialogUtil;
import com.sgcc.ln.pmos.common.utils.UserManager;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.pmos.nativesdk.R;

import java.util.HashMap;
import java.util.Map;

/**
 * 关于我们（意见反馈）
 */
@Route(path = PersonalCenterArouterPath.PERSONALCENTER_FEEDBACK)
public class FeedBackActivity extends MvpBaseActivity {

    private TitleBar mTitleBar;
    private EditText mEtContent;
    private RelativeLayout mRlSubmit;
    private LinearLayout liner_phone;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_feedback_ln;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initViews() {
        super.initViews();
        findViewByIds();
    }

    @Override
    protected void initData() {
        super.initData();
        mTitleBar.setTitle("联系我们");
        mTitleBar.setRightIcon(R.mipmap.go_main, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goMainActivity();
            }
        });
        mEtContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(mEtContent.getText().toString().trim())) {
                    mRlSubmit.setBackground(mContext.getResources().getDrawable(R.drawable.bg_login_btn_3274f0));
                    mRlSubmit.setClickable(true);
                    mRlSubmit.setEnabled(true);
                } else {
                    mRlSubmit.setBackground(mContext.getResources().getDrawable(R.drawable.bg_login_btn_993274f0));
                    mRlSubmit.setClickable(false);
                    mRlSubmit.setEnabled(false);
                }
            }
        });

        mRlSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(mEtContent.getText().toString())) {
                    ToastUtils.showShort("请填写内容");
                } else {
                    if ( UserManager.toLogin()) {

                        postUserMessage(mEtContent.getText().toString());
                    }
                }
            }
        });
    }

    private void findViewByIds() {
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mEtContent = (EditText) findViewById(R.id.et_content);
        mRlSubmit = (RelativeLayout) findViewById(R.id.rl_submit);
        liner_phone =findViewById(R.id.liner_phone);
        liner_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                dialogUtil
                DialogUtil.showConfirm(FeedBackActivity.this, "温馨提示", "您即将跳转至拨号页面", new OnConfirmListener() {
                    @Override
                    public void onConfirm() {
                        startActivity(IntentUtils.getDialIntent("0351-8572288"));
                    }
                });
            }
        });
    }


//    px-shop/userMessageBoard/saveData

    private void postUserMessage(String message) {
        Map<String, Object> map = new HashMap<>();

        map.put("message", message);

        HttpRequestUtils.getInstance().request(map, HttpInterConst.USER_SAVE_MESSAGE, new MvcNetCallback<NoDataResponse>() {
            @Override
            public void onResponse(NoDataResponse response, int id) {
                super.onResponse(response, id);
                if (null != response) {
                    if (response.getCode() == 200) {
                        mEtContent.setText("");
                        ToastUtils.showShort("提交成功");
//                        finish();
                    } else {
                        ToastUtils.showShort(response.getMsg());
                    }
                } else {
                    ToastUtils.showShort("提交失败");
                }
            }
        });
    }

}
